import Vue from 'vue'
import VueRouter from 'vue-router'
import LogIn from '../components/Login'
import HoMe from '../components/home'
import WelCome from '../views/weclome'
import UsErs from '../views/users'
import RigHts from '../views/rights'
import RoLes from '../views/roles'
import GooDs from '../views/goods'
import CateGories from '../views/categories'
import paRams from '../views/params'
import aDd from '../views/add'
import orDers from '../views/orders'
import rePorts from '../views/reports'

Vue.use(VueRouter)

const routes = [
  // 路由重新定向，使得用户直接进入登录页面
  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '/login',
    component: LogIn,
  },
  {
    path: '/home',
    component: HoMe,
    redirect: '/weclome',
    children: [
      {
        path: '/weclome',
        component: WelCome,
      },
      {
        path: '/users',
        component: UsErs,
      },
      {
        path: '/rights',
        component: RigHts,
      },
      {
        path: '/roles',
        component: RoLes,
      },
      {
        path: '/goods',
        component: GooDs,
      },
      {
        path: '/categories',
        component: CateGories,
      },
      {
        path: '/params',
        component: paRams,
      },
      {
        path: '/goods/add',
        component: aDd,
      },
      {
        path: '/orders',
        component: orDers,
      },
      {
        path: '/reports',
        component: rePorts,
      },
    ],
  },
]

const router = new VueRouter({
  routes,
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path == '/login') return next()
  const token = window.sessionStorage.token
  if (!token) return next('/login')
  next()
})

export default router
